from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.select import Select
from time import  sleep
# 创建浏览器驱动对象
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

# 1. 获取‘注册实例.html’当前窗口句柄
# 2. 点击‘注册实例.html’页面中注册A页面
# 3. 获取所有窗口句柄
# 4. 获取注册A页面对应的窗口句柄，并切换
# 5. 操作注册A页面元素
driver = webdriver.Chrome()
# 加载web页面
uil=r"E:\PyCharm 2021\pythonProject\UI自动\注册实例.html";
driver.get(uil)
# 获取‘注册实例.html’当前窗口句柄
# 目的:判断只要不是当前窗句柄，就一定是新的窗口句柄
current_handle=driver.current_window_handle


# 1). 填写主页面的注册信息

driver.find_element_by_css_selector("#user").send_keys("admin")
driver.find_element_by_css_selector("#password").send_keys("123456")
driver.find_element_by_css_selector("#tel").send_keys("13512345678")
driver.find_element_by_css_selector("#email").send_keys("123@QQ.com")


# 获取‘注册实例.html’当前窗口句柄
# 目的:判断只要不是当前窗句柄，就一定是新的窗口句柄
current_handle=driver.current_window_handle
print("当前窗口句柄",current_handle)
# 2). 填写注册页面A中的注册信息
# 切换到注册A,点击注册A链接
driver.find_element_by_partial_link_text("A网页").click()

#  3.获取所有窗口句柄
handles = driver.window_handles
print("所有窗口句柄",handles)
#  获取注册A页面对应的窗口句柄，并切换
for i in  handles:
    if i !=current_handle:
        # 切换
        driver.switch_to.window(i)



sleep(2)
driver.find_element_by_css_selector("#userA").send_keys("admin2")
driver.find_element_by_css_selector("#passwordA").send_keys("123456")
driver.find_element_by_css_selector("#telA").send_keys("13522345678")
driver.find_element_by_css_selector("#emailA").send_keys("123@qq.com")


sleep(3)
driver.quit()